package com.xbn.xbnmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.ServerWebExchange;

/**
 * @author XBN
 * @date 2020/7/4 10:18
 * @email 78742541@qq.com
 */
@Configuration
public class XbnmallCorsConfiguration {
    @Bean
public CorsWebFilter corsWebFilter(){
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration corsConfiguration=new CorsConfiguration();
    //配置跨域
    //允许所有请求头
    corsConfiguration.addAllowedHeader("*");
    //允许所有请求方式跨域
    corsConfiguration.addAllowedMethod("*");
    //允许所有来源跨域
    corsConfiguration.addAllowedOrigin("*");
    //允许携带 token,设置为false就会丢失cookies信息
    corsConfiguration.setAllowCredentials(true);
    ///**所有跨域地址都需要被检查，所有跨域配置都放在corsConfiguration中
    source.registerCorsConfiguration("/**",corsConfiguration);
    return new CorsWebFilter(source);
}
}
